/**
 * Created by yuweimei on 2018/3/28.
 */
app.controller('register', function ($scope, $http) {
    //用户邮箱
    $scope.email = '';
    //用户登录密码
    $scope.password = '';
    //验证码
    $scope.verificationCode = '';
    $scope.emailRegister = function () {
        var registerInfo = {
            'email': $scope.email,
            'password': $scope.password,
            'verificationCode': $scope.verificationCode
        };
        $http({
            method: 'post',
            url: app.netPath + '/users/register/email',
            params: registerInfo
        }).then(function (res) {
            if (!res.data.error) {
                $.toast('注册成功,请申请审核！');
                //成功后自动登录
                // var loginInfo = {
                // 	'username': $scope.email,
                // 	'password': $scope.password
                // };
                // $http({
                // 	method: 'post',
                // 	url: app.netPath + '/users/login',
                // 	params: loginInfo
                // }).then(function(res) {
                // 	console.log(res);
                // 	if(!res.data.error) {
                // 		localStorage.setItem('token', res.data.data);
                // 		location.href = '#home';
                // 	} else {
                // 		$.toast(res.data.msg);
                // 	}
                // }, function() {
                // 	$.toast('网络或设置错误');
                // });
                location.href = '#verify'

            } else {
                var error = res.data.msg;
                $.toast(error)
            }
        }, function () {
            $.toast('网络走丢了！')
        })
    };
    $scope.getVerificationCode = function () {
        var email = {
            'email': $scope.email
        };
        $http({
            method: 'get',
            url: app.netPath + '/users/getVerificationCode',
            params: email
        }).then(function (res) {
            if (!res.data.error) {
                $.toast('验证码发送成功');
            } else {
                $.toast(res.data.msg)
            }
        }, function (res) {
            console.log(res);
            $.alert('网络有问题哟');
        })
    }
    $(".get_emil_code").click(function () {
        if ($(".email_input input[ type='text']").val() == "") {
            $.toast("邮箱不能为空");
            return false;

        } else if (!(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/).test($(".email_input input[ type='text']").val())) {

            $.toast("邮箱格式不正确！请重新输入");
            $(".email1").focus();

        } else {
            $.toast("获取成功");
            $scope.getVerificationCode();

        }

    })
    $(".register_bottom").click(function () {
        var psd1 = $("input[name='psd1']").val();
        var psd2 = $("input[name='psd2']").val();
        if (psd1 != psd2) {
            $.toast("两次密码不一致");
        }
        if (psd1 == psd2) {
            $scope.emailRegister();
        }
    })
})